package com.unhz.taigu.controller;

import com.unhz.generator.pojo.InsoleDict;
import com.unhz.generator.utils.JsonResult;
import com.unhz.generator.utils.Page;
import com.unhz.generator.utils.PageSize;
import com.unhz.taigu.service.InsoleDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @Author: 沈佳庆
 * @Description: 中底数据字典
 * @Date: Created in 下午 6:32 2018/1/29 0029
 * @Modified By:
 */
@Controller
@RequestMapping("/insoleDicts")
public class InsoleDictController {

    private Logger logger =  LoggerFactory.getLogger(InsoleDictController.class);

    @Autowired
    private InsoleDictService insoleDictService;

    /**
     * 传统业务字典分页查询
     * @param model
     * @param pageNo
     * @param keywords
     * @return
     */
    @RequestMapping("/findAllTradition")
    public String findAllTradition(Model model,
                                    @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                    @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords){
        Page<InsoleDict> page = insoleDictService.selectPageTradition(pageNo, PageSize.pageSize,keywords);
        List<InsoleDict> list = insoleDictService.selectAllInsoleDict();
        model.addAttribute("page",page);
        model.addAttribute("list",list);
        model.addAttribute("keywords",keywords);
        return "insoleDict/tradition/index.jsp";
    }

    /**
     * 注塑业务字典分页查询
     * @param model
     * @param pageNo
     * @param keywords
     * @return
     */
    @RequestMapping("/findAllInjection")
    public String findAllInjection(Model model,
                                    @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                    @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords){
        Page<InsoleDict> page = insoleDictService.selectPageInjection(pageNo, PageSize.pageSize,keywords);
        List<InsoleDict> list = insoleDictService.selectAllInsoleDict();
        model.addAttribute("list",list);
        model.addAttribute("page",page);
        model.addAttribute("keywords",keywords);
        return "insoleDict/injection/index.jsp";
    }

    /**
     * PU一体业务字典分页查询
     * @param model
     * @param pageNo
     * @param keywords
     * @return
     */
    @RequestMapping("/findAllResin")
    public String findAllResin(Model model,
                                    @RequestParam(value = "pageNo",defaultValue = "1",required = false)String pageNo,
                                    @RequestParam(value = "keywords",defaultValue = "",required = false)String keywords){
        Page<InsoleDict> page = insoleDictService.selectPageResin(pageNo, PageSize.pageSize,keywords);
        List<InsoleDict> list = insoleDictService.selectAllInsoleDict();
        model.addAttribute("list",list);
        model.addAttribute("page",page);
        model.addAttribute("keywords",keywords);
        return "insoleDict/resin/index.jsp";
    }

    /**
     * 跳转到修改中底业务字典页面
     * @param model
     * @param insoleDictId
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,@RequestParam(value = "insoleDictId",required = false)Integer insoleDictId){
        InsoleDict insoleDict = insoleDictService.selectInsoleDict(insoleDictId);
        model.addAttribute("insoleDict",insoleDict);
        return "insoleDict/update.jsp";
    }

    /**
     * 修改中底业务字典
     * @param insoleDict
     * @return
     */
    @RequestMapping("/update")
    public String update(InsoleDict insoleDict,@RequestParam(value = "adminName",required = false)String adminName){
        insoleDict.setUpdateTime(new Date());
        insoleDict.setUpdateBy(adminName);
        insoleDictService.updateInsoleDict(insoleDict);
        if(1==insoleDict.getType()){
            return "redirect:findAllTradition";
        }else if(2==insoleDict.getType()){
            return "redirect:findAllInjection";
        }else{
            return "redirect:findAllResin";
        }
    }

    @RequestMapping("/isenable")
    @ResponseBody
    public JsonResult isenable(@RequestParam(value = "adminName",required = false)String adminName,
                           @RequestParam(value = "isenable",required = false)Integer isenable,
                           @RequestParam(value = "insoleDictId",required = false)Integer insoleDictId){
        String index = insoleDictService.updateIsenable(insoleDictId, isenable,adminName);
        if("success".equals(index)){
            return JsonResult.success("操作成功");
        }
        return JsonResult.failure("操作失败");
    }



}
